package service

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gtime"
	"tFilm4/internal/model"
	"tFilm4/internal/model/entity"
	"tFilm4/internal/service/internal/dao"
	"tFilm4/internal/service/internal/do"
)

type sMovie struct{}

var insMovie = sMovie{}

func Movie() *sMovie {
	return &insMovie
}

func (s *sMovie) CreateMovie(ctx context.Context, in model.CreateMovieInput) (err error) {
	_, err = dao.Movie.Ctx(ctx).Data(do.Movie{
		MovieName:        in.MovieName,
		MovieDirector:    in.MovieDirector,
		MovieActor:       in.MovieActor,
		MovieImage:       in.MovieImage,
		MovieDescription: in.MovieDescription,
		MovieDuration:    in.MovieDuration,
		MovieShowTime:    in.MovieShowTime,
	}).Insert()
	return
}

func (s *sMovie) GetMovieList(ctx context.Context, in model.GetMovieInput) (count int, movies []entity.Movie, err error) {
	err = dao.Movie.Ctx(ctx).WhereLike("movie_id", "%"+in.SearchKey+"%").WhereOrLike("movie_name", "%"+in.SearchKey+"%").Page(in.PageNum, in.PageSize).Scan(&movies)
	count, err = dao.Movie.Ctx(ctx).Count()
	return
}
func (s *sMovie) UpdateMovie(ctx context.Context, in entity.Movie) (err error) {
	_, err = dao.Movie.Ctx(ctx).Data(in).Where("movie_id", in.MovieId).Update()
	return
}
func (s *sMovie) GetMovieById(ctx context.Context, movieId int) (movie *entity.Movie, err error) {
	err = dao.Movie.Ctx(ctx).WherePri(movieId).Scan(&movie)
	return
}
func (s *sMovie) GetMovieListByDate(ctx context.Context, time gtime.Time) (count int, movies []entity.Movie, err error) {
	err = dao.Movie.Ctx(ctx).Where("movie_show_time", time).Scan(&movies)
	count, err = dao.Movie.Ctx(ctx).Count()
	return
}

func (s *sMovie) GetShowingMovieList(ctx context.Context) (count int, movies []entity.Movie, err error) {
	err = dao.Movie.Ctx(ctx).Where(g.Map{
		"movie_show_time<=": gtime.Now(),
	}).Scan(&movies)
	count, err = dao.Movie.Ctx(ctx).Where(g.Map{
		"movie_show_time<=": gtime.Now(),
	}).Count()
	return
}

func (s *sMovie) GetComingMovieList(ctx context.Context) (count int, movies []entity.Movie, err error) {
	err = dao.Movie.Ctx(ctx).Where(g.Map{
		"movie_show_time>=": gtime.Now(),
	}).Scan(&movies)
	count, err = dao.Movie.Ctx(ctx).Where(g.Map{
		"movie_show_time>=": gtime.Now(),
	}).Count()
	return
}
